package com.lyw.gulimall.product.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyw.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lyw.gulimall.product.dao.BrandDao;
import com.lyw.gulimall.product.entity.BrandEntity;
import com.lyw.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public IPage<Map<String, Object>> queryPage(Integer page, Integer limit, String name) {

        Page<Map<String, Object>> result = new Page<>(page, limit);
        LambdaQueryWrapper<BrandEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BrandEntity::getShowStatus, '1');
        if (StringUtils.isNotBlank(name)) {
            lqw.eq(BrandEntity::getName, name);
        }
        IPage<Map<String, Object>> list = baseMapper.selectPageList(result, lqw);
        return list;
    }

    @Override
    public void updateShowStatusById(BrandEntity brand) {
        baseMapper.updateShowStatusById(brand);
    }

    @Override
    // 开启事务
    @Transactional
    public void updateDetail(BrandEntity brand) {
        // 保证冗余字段的数据一致性，也就是分类品牌关联表中的名字一致性
        // 先修改自己的表
        this.updateById(brand);
        // 修改冗余的表
        if (StringUtils.isNotBlank(brand.getName())) {
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
        }
    }

}
